<template>
    <el-menu default-active="1-4-1" class="el-menu-vertical-demo" @open="handleOpen" @close="handleClose"
        :collapse="isCollapse" background-color="#545c64" text-color="#fff" active-text-color="#ffd04b">
        <h3>{{ isCollapse ? '后台':'仓库管理系统' }}</h3>
        <el-menu-item @click="menuClick(item)" v-for="item in noChildren" :key="item.name" :index="item.name">
            <i :class="`el-icon-${item.icon}`"></i>
            <span slot="title">{{ item.label }}</span>
        </el-menu-item>
        <el-submenu v-for="item in hasChildren" :key="item.label" :index="item.label">
            <template slot="title">
                <i :class="`el-icon-${item.icon}`"></i>
                <span slot="title">{{ item.label }}</span>
            </template>
            <el-menu-item-grou @click="menuClick(subItem)" v-for="subItem in item.children" :key="subItem.name">
                <el-menu-item :index="subItem.name">{{ subItem.label }}</el-menu-item>
            </el-menu-item-grou>

        </el-submenu>

    </el-menu>


</template>
<style lang="less" scoped>
    .el-menu-vertical-demo:not(.el-menu--collapse) {
        width: 200px;
        min-height: 400px;
    }

    .el-menu {
        height: 100vh;

        h3 {
            color: aliceblue;
            text-align: center;
        }

    }
</style>

<script>
    import Cookie from 'js-cookie'

    export default {
        data() {
            return {
                // 管理员路由数据
                adminData: [
                {
                        path: '/',
                        name: 'home',
                        label: '首页',
                        icon: 's-home',
                        url: 'Home/home'
                    },
                    {
                        path: '/factory',
                        name: 'factory',
                        label: '厂家管理',
                        icon: 'office-building',
                        url: 'Factory/factory'
                    },
                    {
                        path: '/user',
                        name: 'user',
                        label: '用户管理',
                        icon: 'user',
                        url: 'UserManage/UserManage'
                    },
                    {
                        label: '商品管理',
                        icon: 's-goods',
                        children: [
                            {
                                path: '/product',
                                name: 'product',
                                label: '商品信息',
                                icon: 'setting',
                                url: 'product/product'
                            },
                            {
                                path: '/category',
                                name: 'category',
                                label: '商品类别',
                                icon: 'setting',
                                url: 'category/category'
                            },
                            {
                                path: '/unit',
                                name: 'unit',
                                label: '计量单位',
                                icon: 'setting',
                                url: 'unit/unit'
                            },
                        ]
                    },
                    {
                        label: '仓库管理',
                        icon: 'truck',
                        children: [
                            {
                                path: '/wStore',
                                name: 'wStore',
                                label: '仓库信息',
                                icon: 'setting',
                                url: 'wStore/wStore'
                            },
                            {
                                path: '/stock',
                                name: 'stock',
                                label: '仓库库存',
                                icon: 'setting',
                                url: 'stock/stock'
                            },
                            {
                                path: '/sPermission',
                                name: 'sPermission',
                                label: '仓库权限',
                                icon: 'setting',
                                url: 'sPermission/sPermission'
                            },
                        ]
                    },
                    {
                        label: '出入库管理',
                        icon: 'truck',
                        children: [
                            {
                                path: '/entryStore',
                                name: 'entryStore',
                                label: '商品入库',
                                icon: 'setting',
                                url: 'entryStore/entryStore'
                            },
                            {
                                path: '/outStore',
                                name: 'outStore',
                                label: '商品出库',
                                icon: 'setting',
                                url: 'outStore/outStore'
                            },
                            {
                                path: '/returnStore',
                                name: 'returnStore',
                                label: '退回仓库',
                                icon: 'setting',
                                url: 'returnStore/returnStore'
                            },
                        ]
                    },
                ],
                // 普通用户路由数据
                userData: [
                {
                        path: '/',
                        name: 'home',
                        label: '首页',
                        icon: 's-home',
                        url: 'Home/home'
                    },
                    {
                        path: '/factory',
                        name: 'factory',
                        label: '厂家管理',
                        icon: 'office-building',
                        url: 'Factory/factory'
                    },
                    {
                        label: '商品管理',
                        icon: 's-goods',
                        children: [
                            {
                                path: '/product',
                                name: 'product',
                                label: '商品信息',
                                icon: 'setting',
                                url: 'product/product'
                            },
                            {
                                path: '/category',
                                name: 'category',
                                label: '商品类别',
                                icon: 'setting',
                                url: 'category/category'
                            },
                            {
                                path: '/unit',
                                name: 'unit',
                                label: '计量单位',
                                icon: 'setting',
                                url: 'unit/unit'
                            },
                        ]
                    },
                    {
                        label: '仓库管理',
                        icon: 'truck',
                        children: [
                            {
                                path: '/wStore',
                                name: 'wStore',
                                label: '仓库信息',
                                icon: 'setting',
                                url: 'wStore/wStore'
                            },
                            {
                                path: '/stock',
                                name: 'stock',
                                label: '仓库库存',
                                icon: 'setting',
                                url: 'stock/stock'
                            },
                        ]
                    },
                    {
                        label: '出入库管理',
                        icon: 'truck',
                        children: [
                            {
                                path: '/entryStore',
                                name: 'entryStore',
                                label: '商品入库',
                                icon: 'setting',
                                url: 'entryStore/entryStore'
                            },
                            {
                                path: '/outStore',
                                name: 'outStore',
                                label: '商品出库',
                                icon: 'setting',
                                url: 'outStore/outStore'
                            },
                            {
                                path: '/returnStore',
                                name: 'returnStore',
                                label: '退回仓库',
                                icon: 'setting',
                                url: 'returnStore/returnStore'
                            },
                        ]
                    },
                ],
                // 页面渲染路由数据
                menuData: []
            };
        },
        methods: {
            // 根据用户权限渲染路由信息
            userMenu(){
                let userType = JSON.parse(Cookie.get('user')).userType
                if(userType==1){
                    this.menuData=this.adminData
                }
                if(userType==2){
                    this.menuData=this.userData
                }
            },
            handleOpen(key, keyPath) {
                console.log(key, keyPath);
            },
            handleClose(key, keyPath) {
                console.log(key, keyPath);
            },
            // 菜单点击事件
            menuClick(item) {
                // 当页面路由与跳转的路由不一致才允许跳转
                if (this.$route.path !== item.path && !(this.$route.path === '/home' && (item.path === '/'))) {
                    this.$router.push(item.path)
                }
                if (this.$store.state.tab.tabsList.length > 1) {
                    this.$store.commit('resetMenu')
                }

                this.$store.commit('selectMenu', item)

            }
        },
        mounted() {
            this.userMenu()
        },
        computed: {

            // 没有子菜单
            noChildren() {
                return this.menuData.filter(item => !item.children)
            },
            // 有子菜单
            hasChildren() {
                return this.menuData.filter(item => item.children)
            },
            // 侧边栏展开与折叠
            isCollapse() {
                return this.$store.state.tab.isCollapse
            }

        },
       
    }
</script>
<style lang="less" scoped>
    .el-menu {
        border-right: none;
    }

    .el-menu-vertical-demo[data-v-a28ad4e6]:not(.el-menu--collapse) {
        min-height: 100%;
    }
</style>